{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Licensed to the Apache Software Foundation (ASF) under one\n",
    "# or more contributor license agreements.  See the NOTICE file\n",
    "# distributed with this work for additional information\n",
    "# regarding copyright ownership.  The ASF licenses this file\n",
    "# to you under the Apache License, Version 2.0 (the\n",
    "# \"License\"); you may not use this file except in compliance\n",
    "# with the License.  You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing,\n",
    "# software distributed under the License is distributed on an\n",
    "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
    "# KIND, either express or implied.  See the License for the\n",
    "# specific language governing permissions and limitations\n",
    "# under the License."
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Fast Sign Adversary Generation Example\n",
    "\n",
    "This notebook demos finds adversary examples using MXNet Gluon and taking advantage of the gradient information\n",
    "\n",
    "[1] Goodfellow, Ian J., Jonathon Shlens, and Christian Szegedy. \"Explaining and harnessing adversarial examples.\" arXiv preprint arXiv:1412.6572 (2014).\n",
    "https://arxiv.org/abs/1412.6572"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "%matplotlib inline\n",
    "import mxnet as mx\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.cm as cm\n",
    "\n",
    "from mxnet import gluon"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Build simple CNN network for solving the MNIST dataset digit recognition task"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "ctx = mx.gpu() if mx.device.num_gpus() else mx.cpu()\n",
    "batch_size = 128"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": true
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Data Loading"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "transform = lambda x,y: (x.transpose((2,0,1)).astype('float32')/255., y)\n",
    "\n",
    "train_dataset = gluon.data.vision.MNIST(train=True).transform(transform)\n",
    "test_dataset = gluon.data.vision.MNIST(train=False).transform(transform)\n",
    "\n",
    "train_data = gluon.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=5)\n",
    "test_data = gluon.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Create the network"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "net = gluon.nn.HybridSequential()\n",
    "with net.name_scope():\n",
    "    net.add(\n",
    "        gluon.nn.Conv2D(kernel_size=5, channels=20, activation='tanh'),\n",
    "        gluon.nn.MaxPool2D(pool_size=2, strides=2),\n",
    "        gluon.nn.Conv2D(kernel_size=5, channels=50, activation='tanh'),\n",
    "        gluon.nn.MaxPool2D(pool_size=2, strides=2),\n",
    "        gluon.nn.Flatten(),\n",
    "        gluon.nn.Dense(500, activation='tanh'),\n",
    "        gluon.nn.Dense(10)\n",
    "    )"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": true
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Initialize training"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "net.initialize(mx.initializer.Uniform(), ctx=ctx)\n",
    "net.hybridize()"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": true
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "loss = gluon.loss.SoftmaxCELoss()"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": true
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1, 'momentum':0.95})"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": true
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Training loop"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "epoch = 3\n",
    "for e in range(epoch):\n",
    "    train_loss = 0.\n",
    "    acc = mx.gluon.metric.Accuracy()\n",
    "    for i, (data, label) in enumerate(train_data):\n",
    "        data = data.as_in_context(ctx)\n",
    "        label = label.as_in_context(ctx)\n",
    "        \n",
    "        with mx.autograd.record():\n",
    "            output = net(data)\n",
    "            l = loss(output, label)\n",
    "            \n",
    "        l.backward()\n",
    "        trainer.update(data.shape[0])\n",
    "        \n",
    "        train_loss += l.mean().item()\n",
    "        acc.update(label, output)\n",
    "    \n",
    "    print(\"Train Accuracy: %.2f\\t Train Loss: %.5f\" % (acc.get()[1], train_loss/(i+1)))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Train Accuracy: 0.92\t Train Loss: 0.32142\n",
      "Train Accuracy: 0.97\t Train Loss: 0.16773\n",
      "Train Accuracy: 0.97\t Train Loss: 0.14660\n"
     ]
    }
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Perturbation\n",
    "\n",
    "We first run a validation batch and measure the resulting accuracy.\n",
    "We then perturbate this batch by modifying the input in the opposite direction of the gradient."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "# Get a batch from the testing set\n",
    "for data, label in test_data:\n",
    "    data = data.as_in_context(ctx)\n",
    "    label = label.as_in_context(ctx)\n",
    "    break\n",
    "\n",
    "# Attach gradient to it to get the gradient of the loss with respect to the input\n",
    "data.attach_grad()\n",
    "with mx.autograd.record():\n",
    "    output = net(data)    \n",
    "    l = loss(output, label)\n",
    "l.backward()\n",
    "\n",
    "acc = mx.gluon.metric.Accuracy()\n",
    "acc.update(label, output)\n",
    "\n",
    "print(\"Validation batch accuracy {}\".format(acc.get()[1]))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Validation batch accuracy 0.96875\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now we perturb the input"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "data_perturbated = data + 0.15 * mx.np.sign(data.grad)\n",
    "\n",
    "output = net(data_perturbated)    \n",
    "\n",
    "acc = mx.gluon.metric.Accuracy()\n",
    "acc.update(label, output)\n",
    "\n",
    "print(\"Validation batch accuracy after perturbation {}\".format(acc.get()[1]))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Validation batch accuracy after perturbation 0.40625\n"
     ]
    }
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Visualization"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Let's visualize an example after pertubation.\n",
    "\n",
    "We can see that the prediction is often incorrect."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "from random import randint\n",
    "idx = randint(0, batch_size-1)\n",
    "\n",
    "plt.imshow(data_perturbated[idx, :].asnumpy().reshape(28,28), cmap=cm.Greys_r)\n",
    "print(\"true label: %d\" % label.asnumpy()[idx])\n",
    "print(\"predicted: %d\" % np.argmax(output.asnumpy(), axis=1)[idx])"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "true label: 1\n",
      "predicted: 3\n"
     ]
    },
    {
     "output_type": "display_data",
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADpJJREFUeJzt3V+IXeW5x/Hfc9JsNb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f23pH9Keszd1yXLvifpPXfflfzHeYW7f7shtT0g6Z91z9ycTCizcuHM0pLukPQ/qvHYpdR1l2o4bnWc+TdIesfd/+zuc5L2SdpaQx2N5+4vSHrvgsVbJe1JHu9R+x9P5brU1gjufsLdX0ken5F0fmbpWo9dSl21qCP8qyT9dcHPx9WsKb9d0kEze9nMdtRdTAcrFsyMdFLSijqL6SBz5uYqXTCzdGOOXT8zXheND/w+atzdb5b0RUk7k5e3jeTt92xN6q7paebmqnSYWfrf6jx2/c54XbQ6wj8tac2Cn1cnyxrB3aeT7zOSnlLzZh8+dX6S1OT7TM31/FuTZm7uNLO0GnDsmjTjdR3hf0nSdWY2YmYtSV+RtL+GOj7CzC5PPoiRmV0uabOaN/vwfknbk8fbJT1dYy0f0pSZm7vNLK2aj13jZrx298q/JG1R+xP/o5K+U0cNXeq6VtL/JV9v1F2bpL1qvwz8QO3PRu6W9ElJhyS9Lek3kpY3qLafqT2b82tqB21lTbWNq/2S/jVJryZfW+o+dil11XLcuMIPCIoP/ICgCD8QFOEHgiL8QFCEHwiK8ANBEX4gKMIPBPUv5DLnMbZADooAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {}
    }
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}